<%@ page import="java.sql.*" %>
<%@ page import="java.lang.*" %>
<%@ page import="java.io.*" %>
<%
try {
String connectionURL = "jdbc:mysql://webproject2.cse.ust.hk/BookwarmMobile?user=BookwarmMobile&password=bookwarm";

Connection connection = null;

Class.forName("com.mysql.jdbc.Driver").newInstance();

connection = DriverManager.getConnection(connectionURL);

if(!connection.isClosed()) {
String newCopyID = null;
String newAccountName = null;

newCopyID = request.getParameter("copyid");
newAccountName = request.getParameter("username");

int count_record = 0;
Statement statement0 = null;
statement0 = connection.createStatement();
String query0 = "UPDATE Sell SET Status = 'Canceled' WHERE AccountName = '" + newAccountName + "' AND CopyID = " + newCopyID + " AND Status = 'Listed';";
count_record = statement0.executeUpdate(query0, Statement.NO_GENERATED_KEYS);
statement0.close();

if(count_record != 0) {
out.print("delete_successful");

Statement statement1 = null;
statement1 = connection.createStatement();
String query1 = "UPDATE BookCopies SET Availability = 'N/A' WHERE CopyID = " + newCopyID + ";";
statement1.executeUpdate(query1, Statement.NO_GENERATED_KEYS);
statement1.close();

Statement statement2 = null;
statement2 = connection.createStatement();
String query2 = "UPDATE Buy SET Status = 'Confirmed' WHERE AccountName = '" + newAccountName + "' AND CopyID = " + newCopyID + " AND Status = 'Canceled' ;";
statement2.executeUpdate(query2, Statement.NO_GENERATED_KEYS);
statement2.close();

}
else {
out.print("delete_failed");

}

}

connection.close();

}
catch(Exception ex){
out.print(ex);

}
%>